package com.sanploy.manage.controller;

import com.sanploy.common.domain.Goods;
import com.sanploy.common.domain.dto.StockEditParamDto;
import com.sanploy.common.domain.vo.GoodsSort;
import com.sanploy.common.domain.vo.PromoteVo;
import com.sanploy.common.vo.BaseException;
import com.sanploy.manage.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by zhu yingzhi on 2017/9/6.
 * @author yingzhi zhu
 * 后台商品接口请求路由类
 */
@RestController
@RequestMapping("/goods")
public class GoodsController
{
    @Autowired
    private GoodsService goodsService;

    /**
     * 添加商品
     * @param cat_id 分类Id
     * @param goods_sn 商品货号
     * @param goods_name 商品名
     * @param goods_number 商品库存
     * @param goods_weight 商品重量
     * @param price 一般价格
     * @param member_price 会员价格
     * @param goods_brief 商品简短描述
     * @param goods_desc 商品详细信息
     * @param pic_banner 商品轮播图
     * @param pic_main 商品代表图
     * @param seller_note 卖家留言
     * @param freight_id 运费模板Id
     * @param goodsAttrs_json 商品规格json
     * @return
     */
    @PostMapping("")
    public Object goodsInsert(Goods goods,@RequestParam Long cat_id, @RequestParam String goods_sn, @RequestParam String goods_name,
                              @RequestParam String goods_number, @RequestParam(defaultValue = "0") double goods_weight, @RequestParam double price,
                              @RequestParam double member_price, @RequestParam String goods_brief, @RequestParam(defaultValue = "") String goods_desc,
                              @RequestParam String pic_banner, @RequestParam String pic_main, @RequestParam String seller_note,
                              @RequestParam Long freight_id, @RequestParam(defaultValue = "") String goodsAttrs_json) throws Exception {

        return  goodsService.insertGoods(goods, goodsAttrs_json);
    }

    /**
     * 编辑商品相关信息
     * @param goods Goods的实体类 由spring自动实例化 并注入值
     * @param id 商品Id
     * @param cat_id 分类Id
     * @param goods_sn 商品货号
     * @param goods_name 商品名
     * @param goods_number 商品库存
     * @param goods_weight 商品重量
     * @param price 一般价格
     * @param member_price 会员价格
     * @param goods_brief 商品简短描述
     * @param goods_desc 商品详细信息
     * @param pic_banner 商品轮播图
     * @param pic_main 商品代表图
     * @param seller_note 卖家留言
     * @param freight_id 运费模板Id
     * @param goodsAttrs_json 商品规格json
     * @return
     * @throws BaseException
     */
    @PostMapping("/{id}")
    public Object goodsUpdate(Goods goods,@PathVariable Long id,@RequestParam Long cat_id, @RequestParam String goods_sn, @RequestParam String goods_name,
                              @RequestParam String goods_number, @RequestParam double price,
                              @RequestParam double member_price, @RequestParam String goods_brief, @RequestParam(defaultValue = "") String goods_desc,
                              @RequestParam String pic_banner, @RequestParam String pic_main, @RequestParam String seller_note,
                              @RequestParam Long freight_id, @RequestParam(defaultValue = "") String goodsAttrs_json
            , @RequestParam(defaultValue = "0") double goods_weight
    ) throws Exception {
        return goodsService.updateGoods(goods,goodsAttrs_json);
    }


    /**
     * 根据商品Id查询商品
     * @param id 商品Id
     * @return
     * @throws BaseException
     */
    @GetMapping("/{id}")
    public Object getOneGoods(@PathVariable Long id) throws BaseException {
        return goodsService.getGoodsById(id);
    }
    /**
     * 分页获取 并按一定规则排序 默认按时间进行排序
     * @param currentPage 当前页数
     * @param pageSize 总页数
     * @param goodsSort 商品分类规则 详见GoodsSort类
     * @return
     */
    @GetMapping("")
    public Object getPage(@RequestParam int currentPage, @RequestParam int pageSize, GoodsSort goodsSort){
        return goodsService.getGoodsPage(currentPage,pageSize,goodsSort);
    }

    /**
     * 根据Id删除商品
     * @param id
     * @return
     * @throws BaseException
     */
    @DeleteMapping("{id}")
    public Object deleteGoods(@PathVariable Long id) throws Exception {
        return goodsService.deleteGoods(id);
    }

    /**
     * 设置商品的促销时间
     * @param promoteVo 数据接收对象
     * @param goods_id 商品Id
     * @param is_promote 是否促销
     * @param promote_price 促销价格
     * @param promote_start_date 促销开始时间 时间格式2017-09-01
     * @param promote_end_date 促销结束时间
     * @return
     */
    @PostMapping("/promote/{goods_id}")
    public Object setGoodsPromote(PromoteVo promoteVo, @PathVariable Long goods_id, @RequestParam double promote_price, @RequestParam boolean is_promote,
                                  @RequestParam String promote_start_date, @RequestParam String promote_end_date) throws BaseException {

        return goodsService.updatePromoteVo(promoteVo);
    }

    /**
     * 查找促销商品的详情
     * @param goods_id
     * @return
     */
    @GetMapping("/promote/{goods_id}")
    public Object getGoodsPromote(@PathVariable Long goods_id){
        return goodsService.getPromoteVo(goods_id);
    }
    /**
     * 商品上下架
     * @param goods_id 商品Id
     * @param is_shelve 是否上架
     * @return
     * @throws BaseException
     */
    @PostMapping("/shelve/{goods_id}")
    public Object setGoodsShelves(@PathVariable Long goods_id,@RequestParam boolean is_shelve) throws BaseException {
        return goodsService.setShelve(is_shelve, goods_id);
    }

    /**
     * 根据名字模糊搜索商品
     * @param search_name
     * @return
     */
    @GetMapping("/search")
    public Object searchGoods(@RequestParam String search_name){
        return goodsService.searchGoodsByName(search_name);
    }

    /**
     * 修改库存 传json
     *
     格式：{
     "stock":[1,2],
     "goodsId":[2,2]
     }
     * @param stockEditParam
     * @return
             */
    @PostMapping("/number")
    public Object editStock(@RequestBody StockEditParamDto stockEditParam){
        return goodsService.editGoodsStock(stockEditParam);
    }



}
